package cn.iocoder.yudao.module.system.job.flow;

import cn.iocoder.yudao.framework.common.util.date.DateUtils;
import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.system.service.config.InfraConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;

/**
 * 项目赛程设置
 */
@Component
@Slf4j
public class ScheduleTypeJob implements JobHandler {

    @Resource
    private InfraConfigService infraConfigService;

    @Override
    @TenantIgnore
    public String execute(String param) throws Exception {
        Integer size=0;
        List<Map<String,Object>> list = infraConfigService.getScheduleTypeData();
        if(CollectionUtils.isNotEmpty(list)){
            size=list.size();
            list.stream()
                    .filter(m->(MapUtils.getInteger(m, "tScheduleType", null)==null
                        || MapUtils.getInteger(m, "tScheduleType", null)!=4))
                    .forEach(m->{
                        Integer scheduleType=MapUtils.getInteger(m,"sScheduleType",null);
                        Long id=MapUtils.getLong(m,"id",null);
                        if(null!=id){
                            infraConfigService.updateProjectScheduleTypeById(id,scheduleType);
                        }
            });
        }
        return String.format("更新定时项目阶段个数 %s 个",size);
    }
}
